/**
 * @param {string} input
 * @return {number}
 */
export var lengthLongestPath = function (input) {
  let max = 0
  const stack = []
  const rows = input.split('\n')
  for (let i = 0; i < rows.length; i++) {
    const row = rows[i]
    if (row.length === 0) continue
    const leve = row.lastIndexOf('\t') + 1
    while (stack.length > leve) {
      stack.pop()
    }
    const name = row.slice(leve)
    stack.push(name)
    if (isFile(name)) {
      max = Math.max(max, stackToPath(stack).length)
    }
  }
  return max
}

function stackToPath(stack) {
  return stack.join('/')
}

function isFile(str) {
  return /\./.test(str)
}
